package com.zw.a.basic.a11.demo04_exercise;

import com.itheima.pojo.Student;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//Map集合案例: 键是String, 值是Student
public class Demo01 {
    public static void main(String[] args) {
        //1.创建HashMap集合, 键是学号(String), 值是学生对象(Student).
        HashMap<String, Student> hm = new HashMap<>();
        //2.往HashMap集合中添加3组数据.
        hm.put("黑马001", new Student("乔峰",36));
        hm.put("黑马002", new Student("虚竹",29));
        hm.put("黑马003", new Student("段誉",21));

        //hm.put("黑马003", new Student("虚竹",29));

        //3.通过两种方式, 遍历HashMap集合.
        //方式一: 根据键获取其对应的键和值.
        Set<String> keys = hm.keySet();
        for (String key : keys) {
            System.out.println(key + "..." + hm.get(key));
        }
        System.out.println("-------------------------");

        //方式二: 根据键值对获取其对应的键和值.
        Set<Map.Entry<String, Student>> entrys = hm.entrySet();
        for (Map.Entry<String, Student> entry : entrys) {
            System.out.println(entry.getKey() + "..." + entry.getValue());
        }
    }
}
